package com.example.springbootdemolianxi002.controller;


import com.example.springbootdemolianxi002.entity.Task;
import com.example.springbootdemolianxi002.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/tasks")
public class TaskController {


    @Autowired
    private  TaskService  taskService;



    @GetMapping
    public List<Task> getAllTask(){
        return     taskService.getallTask();


    }


    @GetMapping("/{id}")
    public  ResponseEntity<Task>  getTaskByID(@PathVariable int id){
        Task task = taskService.getTaskByID(id);
        return  task !=null  ? ResponseEntity.ok(task) : ResponseEntity.notFound().build();
    }



    //新增任务

    @PostMapping
    public  Task  createTask(@RequestBody Task task){
        return  taskService.createTTask(task);
    }


    //修改任务
    @PostMapping("/{id}")
    public  ResponseEntity<Task>  updateTask(@PathVariable int  id,@RequestBody  Task  task){
        Task  t = taskService.updateTask(id, task);
        return  t != null ?  ResponseEntity.ok(task) : ResponseEntity.notFound().build();
    }
    //删除任务


    @DeleteMapping("/{id}")
    public ResponseEntity<Task>  deleteTask(@PathVariable int  id){
        boolean  deleted = taskService.deleteTask(id);
        return   deleted ?  ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }


    //标记任务已经完成

    @PatchMapping("/{id}/completed")
    public  ResponseEntity<Task> iscompleted(@PathVariable int  id){
        Task  task =taskService.markandCompleted(id);
        return  task  != null ? ResponseEntity.ok(task) :  ResponseEntity.notFound().build();


    }
}
